package com.example.service.Impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.enums.ResultCodeEnum;
import com.example.context.BaseContext;
import com.example.mapper.ProblemMapper;
import com.example.pojo.entity.*;
import com.example.service.ProblemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.List;


@Service
@Slf4j
@DS("problem")
public class ProblemServiceImpl extends ServiceImpl<ProblemMapper, Problem> implements ProblemService {
    @Autowired
    private ProblemMapper problemMapper;
    @Override
    public void add(Problem assessment) {
        assessment.setIsDeleted(0);
        assessment.setVersion(0);
        problemMapper.insert(assessment);
    }

    /**
     * 分页查询
     */
    public Page<Problem> selectPage(String name, Integer pageNum, Integer pageSize) {

        Page<Problem> problemPage = problemMapper.selectPage(new Page<Problem>(pageNum, pageSize),
                new LambdaQueryWrapper<Problem>()
                        .like(StringUtils.hasLength(name), Problem::getName,name));
        //.like(StringUtils.hasLength(projectId), Cost::getProjectId, projectId));

        return problemPage;
    }
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            Problem problem = selectById(id);
            problem.setIsDeleted(1);
            problemMapper.updateById(problem);
        }
    }
    @Override
    public void deleteById(Integer id) {
        Problem problem = selectById(id);
        problem.setIsDeleted(1);
        updateProblem(problem);
        problemMapper.updateById(problem);
    }
    private void updateProblem(Problem problem) {
        problemMapper.updateById(problem);
    }

    @Override
    public Problem selectById(Integer id) {
        return problemMapper.selectById(id);
    }

    @Override
    public void update(Problem problem) {
        problemMapper.updateById(problem);
    }


}
